/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.marifnst.itd.preprocessing;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author arifnasution
 */
public class Preprocessing {

    private static Preprocessing preprocessing;
    private ArrayList<String> listOfWord = new ArrayList<>();

    public static Preprocessing getInstance() {
        if (preprocessing == null) {
            preprocessing = new Preprocessing();
        }
        return preprocessing;
    }

    public String textCleanupProcess(String inputString) {
        String temp = inputString;
        try {
            for (String s : UtilPreprocessing.listOfTandaBaca) {
                temp = temp.replaceAll(s, "");
            }
        } catch (Exception exception) {
            Logger.getLogger(Preprocessing.class.getName()).log(Level.SEVERE, null, exception);
        }
        return temp;
    }

    public void tokenizationProcess(String filePath) {
        listOfWord.clear();
        InputStream inputStream = null;
        try {
            File file = new File(filePath);
            inputStream = new FileInputStream(file);
            int output = inputStream.read();
            String word = "";
            while (output != -1) {
                if (output != 32) {
                    word += (char) output;
                } else {
                    listOfWord.add(textCleanupProcess(word).toLowerCase());
                    word = "";
                }
                output = inputStream.read();
            }
            listOfWord.add(textCleanupProcess(word));
        } catch (IOException ex) {
            Logger.getLogger(Preprocessing.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                inputStream.close();
            } catch (IOException ex) {
                Logger.getLogger(Preprocessing.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void preprocessingProcess(String filePath) {
        Preprocessing.getInstance().tokenizationProcess(filePath);

        for (String s : listOfWord) {
//            System.out.println(s);
            System.out.println(s + " " + Stemming.getInstance().stemmingProcess(s));                    
//            System.out.println("===");
        }
    }
}
